package com.cheyun.netsalev3.view.orderquanyi

import android.Manifest
import android.content.Context
import android.os.Bundle
import android.view.View
import android.view.inputmethod.EditorInfo
import android.view.inputmethod.InputMethodManager
import androidx.annotation.Nullable
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProviders
import com.cheyun.netsalev3.R
import com.cheyun.netsalev3.adapter.HomePageAdapter
import com.cheyun.netsalev3.databinding.ActivityOrderQuanyiBinding
import com.cheyun.netsalev3.tencenttim.utils.DemoLog
import com.cheyun.netsalev3.utils.BaseActivity
import com.cheyun.netsalev3.utils.InjectorUtils
import com.cheyun.netsalev3.view.MyScanActivity
import com.cheyun.netsalev3.viewmodel.orderquanyi.OrderQuanyiViewModel
import com.jeremyliao.liveeventbus.LiveEventBus
import com.tbruyelle.rxpermissions3.RxPermissions


class OrderQuanyiActivity : BaseActivity<ActivityOrderQuanyiBinding>() {
    private val BUNDLE_FRAGMENTS_KEY = "android:support:fragments"

    override fun initContentView(): Int {
        return R.layout.activity_order_quanyi
    }

//    var titles = arrayListOf("保养预约", "上门取送车")

    private var searchMap = HashMap<String, String>()

    private var listFragmet = ArrayList<Fragment>()
    var nkeys = arrayListOf("","0","1")

    override fun onCreate(@Nullable savedInstanceState: Bundle?) {
        if (savedInstanceState != null && clearFragmentsTag()) { //重建时清除 fragment的状态
            savedInstanceState.remove(BUNDLE_FRAGMENTS_KEY)
        }
        super.onCreate(savedInstanceState)
    }

    protected fun clearFragmentsTag(): Boolean {
        return true
    }

    override fun initViewModel(): ViewModel? {
        this?.apply {
            val factory = InjectorUtils.getOrderQuanyiListViewModelFactory(nkeys)
            return ViewModelProviders.of(this, factory).get(OrderQuanyiViewModel::class.java)
        }
        return null
    }

    override fun initData() {

    }

    override fun initView() {
        listFragmet.add(OrderQuanyiFragment())
//        listFragmet.add(AftersaleDriveFragment())
        val pagerAdapter = HomePageAdapter(supportFragmentManager, listFragmet)
//        pagerAdapter.titles = titles
//        binding.viewPager.offscreenPageLimit = 2
        binding.viewPager.adapter = pagerAdapter
//        binding.tabLayout.setupWithViewPager(binding.viewPager)

        binding.searchText.setOnEditorActionListener { v, actionId, event ->
            if (actionId == EditorInfo.IME_ACTION_SEARCH || actionId == EditorInfo.IME_ACTION_UNSPECIFIED) {
                //关闭软键盘
                closeKeyword()
                //do something
                //doSearch();
                searchMap = HashMap<String, String>()
                searchMap["keyword"] = binding.searchText.text.toString()

                refresh()
                true
            } else
                false
        }
        binding.leftBut.setOnClickListener{
            finish()
        }

        binding.lyscan.setOnClickListener(View.OnClickListener {
            RxPermissions(this).requestEach(
                    Manifest.permission.CAMERA,
                    Manifest.permission.SYSTEM_ALERT_WINDOW
            ).subscribe {
                if (it.granted) {

                    LiveEventBus.get().with("scanReal").postValue("")
                }else if (it.shouldShowRequestPermissionRationale) {
                    DemoLog.e("camera", "shouldShowRequestPermissionRationale");
                } else {
                    DemoLog.e("camera", "error");
                    MyToast("请确认是否已禁止应用使用手机相机功能")
                }
            }

        })

    }

    fun closeKeyword(){
        val view = window.peekDecorView()
        if (view != null) {
            val inputmanger = this.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
            inputmanger.hideSoftInputFromWindow(view.windowToken, 0)
        }
    }

    fun refresh(){
        var hashMap = HashMap<String, String>()

        searchMap.forEach {
            hashMap[it.key] = it.value
        }
        (listFragmet[0] as OrderQuanyiFragment).setSearch(hashMap)
//        (listFragmet[1] as AftersaleDriveFragment).setSearch(hashMap)

    }
}
